{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# traffic_lights"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/traffic_lights.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/traffic_lights.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Traffic lights problem in Google CP Solver.\n",
    "\n",
    "  CSPLib problem 16\n",
    "  http://www.cs.st-andrews.ac.uk/~ianm/CSPLib/prob/prob016/index.html\n",
    "  '''\n",
    "  Specification:\n",
    "  Consider a four way traffic junction with eight traffic lights. Four of the\n",
    "  traffic\n",
    "  lights are for the vehicles and can be represented by the variables V1 to V4\n",
    "  with domains\n",
    "  {r,ry,g,y} (for red, red-yellow, green and yellow). The other four traffic\n",
    "  lights are\n",
    "  for the pedestrians and can be represented by the variables P1 to P4 with\n",
    "  domains {r,g}.\n",
    "\n",
    "  The constraints on these variables can be modelled by quaternary constraints\n",
    "  on\n",
    "  (Vi, Pi, Vj, Pj ) for 1<=i<=4, j=(1+i)mod 4 which allow just the tuples\n",
    "  {(r,r,g,g), (ry,r,y,r), (g,g,r,r), (y,r,ry,r)}.\n",
    "\n",
    "  It would be interesting to consider other types of junction (e.g. five roads\n",
    "  intersecting) as well as modelling the evolution over time of the traffic\n",
    "  light sequence.\n",
    "  ...\n",
    "\n",
    "  Results\n",
    "  Only 2^2 out of the 2^12 possible assignments are solutions.\n",
    "\n",
    "  (V1,P1,V2,P2,V3,P3,V4,P4) =\n",
    "     {(r,r,g,g,r,r,g,g), (ry,r,y,r,ry,r,y,r), (g,g,r,r,g,g,r,r),\n",
    "     (y,r,ry,r,y,r,ry,r)}\n",
    "     [(1,1,3,3,1,1,3,3), ( 2,1,4,1, 2,1,4,1), (3,3,1,1,3,3,1,1), (4,1, 2,1,4,1,\n",
    "     2,1)}\n",
    "\n",
    "   The problem has relative few constraints, but each is very tight. Local\n",
    "   propagation\n",
    "  appears to be rather ineffective on this problem.\n",
    "\n",
    "  '''\n",
    "\n",
    "  Note: In this model we use only the constraint solver.AllowedAssignments().\n",
    "\n",
    "\n",
    "  Compare with these models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/traffic_lights.mzn\n",
    "  * Comet   : http://www.hakank.org/comet/traffic_lights.co\n",
    "  * ECLiPSe : http://www.hakank.org/eclipse/traffic_lights.ecl\n",
    "  * Gecode  : http://hakank.org/gecode/traffic_lights.cpp\n",
    "  * SICStus : http://hakank.org/sicstus/traffic_lights.pl\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main(base=10, start=1, len1=1, len2=4):\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver(\"Traffic lights\")\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 4\n",
    "  r, ry, g, y = list(range(n))\n",
    "  lights = [\"r\", \"ry\", \"g\", \"y\"]\n",
    "\n",
    "  # The allowed combinations\n",
    "  allowed = []\n",
    "  allowed.extend([(r, r, g, g), (ry, r, y, r), (g, g, r, r), (y, r, ry, r)])\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  V = [solver.IntVar(0, n - 1, \"V[%i]\" % i) for i in range(n)]\n",
    "  P = [solver.IntVar(0, n - 1, \"P[%i]\" % i) for i in range(n)]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  for i in range(n):\n",
    "    for j in range(n):\n",
    "      if j == (1 + i) % n:\n",
    "        solver.Add(solver.AllowedAssignments((V[i], P[i], V[j], P[j]), allowed))\n",
    "\n",
    "  #\n",
    "  # Search and result\n",
    "  #\n",
    "  db = solver.Phase(V + P, solver.INT_VAR_SIMPLE, solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    for i in range(n):\n",
    "      print(\"%+2s %+2s\" % (lights[V[i].Value()], lights[P[i].Value()]), end=\" \")\n",
    "    print()\n",
    "    num_solutions += 1\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print(\"num_solutions:\", num_solutions)\n",
    "  print(\"failures:\", solver.Failures())\n",
    "  print(\"branches:\", solver.Branches())\n",
    "  print(\"WallTime:\", solver.WallTime())\n",
    "  print()\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
